<?php

namespace App\Http\Controllers\Guard\Article;

use App\Http\Controllers\Common\GuardController;
use App\Models\Article\ArticleClassify;
use App\Models\Article\ArticleInfo;
use App\Models\Constant\Constant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class ArticleInfoController extends GuardController {

    public function lst(Request $request) {
        $classifies = ArticleClassify::pluck('name','id')->toArray();
        try{
            $page = $request->input('page',1);
            $pageSize = $request->input('pageSize',Constant::PAGE_SIZE);
            $title = $request->input('title','');
            $keyWord = $request->input('keyWord','');
            $classifyId = $request->input('classifyId','all');
            $conditions = compact('keyWord','classifyId','title');
            $conditions['orderBy']['col'] = 'sort';
            $conditions['orderBy']['sort'] = 'desc';

            $records = (new ArticleInfo)->getLst($conditions)->paginate($pageSize,['*'],'page',$page);
            foreach ($records as $record) {
                $record->description = Str::limit($record->description,20);
                $record->content = Str::limit($record->content,20);
                $record->up_down_name = $record->up_down==1?'是':'否';
                $record->is_top_name = $record->is_top==1?'是':'否';
                $record->classify_name = $classifies[$record->classify_id]??'未知';
            }
            return responseSuccess($records->items(),$records->total());
        }catch(\Exception $e){
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }

    public function add(Request $request) {

        try{
            $id = $request->input('id',0);
            $info = $request->input();
            ArticleInfo::updateOrCreate(compact('id'),$info);
            return responseSuccess();
        }catch(\Exception $e){
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }

    public function getRecord(Request $request) {
        return $this->getRecordByIds(ArticleInfo::class, $request->input('id','')); // TODO: Change the autogenerated stub
    }

    public function del(Request $request) {
        return $this->delRecords(ArticleInfo::class, $request->input('id',''));
    }

    public function uploadVideo(Request $request) {
        $fileKeys = array_keys($request->file());
        foreach ($fileKeys as $fileKey) {
            $file = $request->file($fileKey);
            $ext = $file->getClientOriginalExtension();
            $middlePath = "/article/".date('Y-m-d');
            $absDir = Storage::disk('upload')->path($middlePath);
            if(!is_dir($absDir)){mkdir($absDir,0777,true);}
            $fileName = time().uniqid().".$ext";
            $filePath = $file->storeAs($middlePath,$fileName,'upload');
            $filePath = '/storage/upload/'.$filePath;
            $info= [
                'url'=>asset($filePath),
            ];
            return response()->json([
                'data'=>$info,
                'errno'=>0
            ]);
        }
    }

    public function uploadImg(Request $request) {
        $fileKeys = array_keys($request->file());
        $info = [];
        foreach ($fileKeys as $fileKey) {
            $file = $request->file($fileKey);
            $ext = $file->getClientOriginalExtension();
            $middlePath = "/article/".date('Y-m-d');
            $absDir = Storage::disk('upload')->path($middlePath);
            if(!is_dir($absDir)){mkdir($absDir,0777,true);}
            $fileName = time().uniqid().".$ext";
            $filePath = $file->storeAs($middlePath,$fileName,'upload');
            $filePath = '/storage/upload/'.$filePath;
            $info[] = [
                'url'=>asset($filePath),
                'atl'=>'图片',
                'href'=>''
            ];

        }
        return response()->json([
            'data'=>$info,
            'errno'=>0
        ]);

    }

}
